package logic

import (
	"cart_srv/internal/svc"
	"cart_srv/model/mysql"
	"cart_srv/model/redis"
	"cart_srv/pb/cart"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodsLogic) AddGoods(in *cart.AddGoodsRequest) (*cart.AddGoodsResponse, error) {
	// todo: add your logic here and delete this line

	O := mysql.Goods{
		GoodsTitle:  in.GoodsTitle,
		GoodsCover:  in.GoodsCover,
		GoodsPrice:  float64(in.GoodsPrice),
		GoodsStock:  int(in.GoodsStock),
		GoodsStatus: int(in.GoodsStatus),
	}
	err := O.Create()
	if err != nil {
		logx.Error("添加失败")
		return nil, err
	}
	redis.SyncGoodsStock(int64(O.ID), in.GoodsStock)

	return &cart.AddGoodsResponse{Success: true}, nil
}
